package com.keya.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

@Slf4j
public class URLUtil {

    public static String replaceDomainAndPort(String domain, String port, String url) {
        StringBuilder url_bak = new StringBuilder();
        if (url.contains("//")) {
            String[] splitTemp = url.split("//");
            if (domain.contains("://")) {
                String[] split = domain.split("://");
                url_bak = new StringBuilder(split[0] + "://");
                if (StringUtils.isNotEmpty(port)) {
                    url_bak.append(split[1]).append(":").append(port);
                } else {
                    url_bak.append(split[1]);
                }
            } else {
                url_bak = new StringBuilder(splitTemp[0] + "//");
                if (StringUtils.isNotEmpty(port)) {
                    url_bak.append(domain).append(":").append(port);
                } else {
                    url_bak.append(domain);
                }
            }


            if (splitTemp.length >= 1 && splitTemp[1].contains("/")) {
                String[] urlTemp2 = splitTemp[1].split("/");
                if (urlTemp2.length > 1) {
                    for (int i = 1; i < urlTemp2.length; i++) {
                        url_bak.append("/").append(urlTemp2[i]);
                    }
                }
            }
        }
        return url_bak.toString();
    }

    public static void main(String[] args) {
        String s = replaceDomainAndPort("abcjskjf-bdjbf/aaa", "", "http://192.168.0.3:7777/api/bbb");
        System.out.println(s);
    }
}
